package com.sunny.framework.system.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.framework.core.base.action.BaseAction;
import com.sunny.framework.system.entity.Menu;
import com.sunny.framework.system.service.MenuService;

@Controller
@RequestMapping("system/menu.html")
public class MenuAction extends BaseAction<Menu>{

	private Logger logger = Logger.getLogger(MenuAction.class);
	
	@Autowired
	private MenuService service;
	
	@RequiresPermissions("menu:list")
	@RequestMapping(params="list")
	@Override
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		logger.info("list");
		return "system/ListMenu";
	}

	@Override
	public String add(HttpServletRequest reuqest, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	@RequestMapping(params="edit")
	@Override
	public @ResponseBody Map<String, Object> edit(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.map_data(service.findById(Long.parseLong(request.getParameter("id"))));
		} catch (Exception e) {
			super.map_eror(e);
		}
		return map;
	}

	@RequestMapping(params="delete")
	@Override
	public @ResponseBody Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			service.deleteByIds(super.stringToLongArray(request.getParameter("id")));
			super.map_data();
		} catch (Exception e) {
			super.map_eror(e);
		}
		return map;
	}

	@RequestMapping(params="save")
	@Override
	public @ResponseBody Map<String, Object> save(Model model, HttpServletRequest request,
			HttpServletResponse response,@ModelAttribute Menu menu) {
		try {
			logger.info("id:"+menu.getId());
			logger.info("text:"+menu.getText());
			logger.info("url:"+menu.getUrl());
			logger.info("sort"+menu.getSort());
			logger.info("father_id:"+menu.getFather().getId());
			service.save(menu);
			super.map_data();
		} catch (Exception e) {
			e.printStackTrace();
			super.map_eror(e);
		}
		return map;
	}

	@RequestMapping(params="listJson")
	public @ResponseBody List<Menu> listJson(HttpServletRequest request){
		logger.info("listJson");
		criterions = new ArrayList<Criterion>();
		
		String text = request.getParameter("text");
		if(text!=null&&!text.isEmpty())
			criterions.add(Restrictions.ilike("text", text));

		//查询所有,非叶子的节点
		criterions.add(Restrictions.eq("leaf", false));
		List<Menu> menus = service.list(criterions); 
		return menus;
	}
}
